package com.ruoyi.order.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.WxForm;
import com.ruoyi.order.service.IWxFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * 生成员工提成及报销模版表Controller
 * 
 * @author ruoyi
 * @date 2024-09-04
 */
@RestController
@RequestMapping("/merchant/form")
public class WxFormController extends BaseController
{
    @Autowired
    private IWxFormService wxFormService;
    private static final String FILE_DELIMETER = ",";

    /**
     * 查询生成员工提成及报销模版表列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:form:add')")
    @GetMapping("/list")
    public TableDataInfo list(WxForm wxForm)
    {
        startPage();
        List<WxForm> list = wxFormService.selectWxFormList(wxForm);
        return getDataTable(list);
    }

    /**
     * 导出生成员工提成及报销模版表列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:form:add')")
    @Log(title = "生成员工提成及报销模版表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxForm wxForm)
    {
        List<WxForm> list = wxFormService.selectWxFormList(wxForm);
        ExcelUtil<WxForm> util = new ExcelUtil<WxForm>(WxForm.class);
        util.exportExcel(response, list, "生成员工提成及报销模版表数据");
    }

    /**
     * 获取生成员工提成及报销模版表详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:form:add')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wxFormService.selectWxFormById(id));
    }

    /**
     * 新增生成员工提成及报销模版表
     */
    @PreAuthorize("@ss.hasPermi('merchant:form:add')")
    @Log(title = "生成员工提成及报销模版表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxForm wxForm)
    {
        return toAjax(wxFormService.insertWxForm(wxForm));
    }

    /**
     * 修改生成员工提成及报销模版表
     */
    @PreAuthorize("@ss.hasPermi('merchant:form:add')")
    @Log(title = "生成员工提成及报销模版表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxForm wxForm)
    {
        return toAjax(wxFormService.updateWxForm(wxForm));
    }

    /**
     * 删除生成员工提成及报销模版表
     */
    @PreAuthorize("@ss.hasPermi('merchant:form:add')")
    @Log(title = "生成员工提成及报销模版表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wxFormService.deleteWxFormByIds(ids));
    }

    @GetMapping("/formList")
    public TableDataInfo formList() {
        List<WxForm> list = wxFormService.selectEnableForm();
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('merchant:form:add')")
    @Log(title = "修改表单属性", businessType = BusinessType.UPDATE)
    @PutMapping("/property")
    public AjaxResult editForm(@RequestBody WxForm wxForm) {
        return toAjax(wxFormService.updateWxForm(wxForm));
    }


    /**
     * 油量里程图片上传
     */
    @Log(title = "油量里程图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/fuelfiles")
    @Anonymous
    public AjaxResult fuelFiles(List<MultipartFile> files) throws Exception
    {
        List<String> urls = new ArrayList<String>();
        if (!files.isEmpty())
        {
            for (MultipartFile file : files){
                String fuelfilesurl = FileUploadUtils.upload(RuoYiConfig.getFuelMilPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                urls.add("/prod-api"+fuelfilesurl);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;

        }

        return error("上传图片异常，请联系管理员");
    }


    /**
     * 上传车辆视频接口
     */
    @PostMapping("/uploadVideo")
    @Anonymous
    public AjaxResult uploadVideo(MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String videourl = FileUploadUtils.upload(RuoYiConfig.getVehVideoPath(), file, MimeTypeUtils.VIDEO_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", "/prod-api"+videourl);
            return ajax;
        }
        return error("上传车辆视频异常，请联系管理员");
    }

    /**
     * 订单身份证,驾驶证图片上传
     */
    @Log(title = "订单相关图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/files")
    @Anonymous
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
    {
        List<String> urls = new ArrayList<String>();
        if (!files.isEmpty())
        {
            for (MultipartFile file : files){
                String orderinfourl = FileUploadUtils.upload(RuoYiConfig.getOrderCertiPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                urls.add("/prod-api"+orderinfourl);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;

        }

        return error("上传图片异常，请联系管理员");
    }

    /**
     * 订单车损图片上传
     */
    @Log(title = "订单车损相关图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/carfiles")
    @Anonymous
    public AjaxResult carFiles(List<MultipartFile> files) throws Exception
    {
        List<String> urls = new ArrayList<String>();
        if (!files.isEmpty())
        {
            for (MultipartFile file : files){
                String vehdamageurl = FileUploadUtils.upload(RuoYiConfig.getVehDamagePath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                urls.add("/prod-api"+vehdamageurl);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;

        }

        return error("上传图片异常，请联系管理员");
    }

    /**
     * 订单维修发票图片上传
     */
    @Log(title = "订单维修发票图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/invoicefiles")
    @Anonymous
    public AjaxResult invoiceFiles(List<MultipartFile> files) throws Exception
    {
        List<String> urls = new ArrayList<String>();
        if (!files.isEmpty())
        {
            for (MultipartFile file : files){
                String vehinvoiceurl = FileUploadUtils.upload(RuoYiConfig.getVehInvoicePath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                urls.add("/prod-api"+vehinvoiceurl);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;

        }
        return error("上传图片异常，请联系管理员");
    }

}
